<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CreateCustomerCouponsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customer_coupons', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('userId')->default(0)->comment('用户id');
            $table->unsignedBigInteger('activityId')->default(0)->comment('活动id');
            $table->unsignedBigInteger('couponId')->default(0)->comment('优惠券id');
            $table->string('couponCode')->default('')->comment('用户优惠券编码');
            $table->string('name',50)->default('')->comment('优惠券名称');
            $table->string('desc',50)->default('')->comment('优惠券描述');
            $table->unsignedTinyInteger('couponType')->default(1)->comment('优惠券类型 1:代金券 2商品兑换券 3 折扣券');
            $table->unsignedTinyInteger('useTimeType')->default(1)->comment('使用有效期  1 固定时间 2 相对时间');
            $table->string('canUseProduct')->default(0)->comment('可用商品 0 全部可用 1：部分可用是json串');
            $table->unsignedInteger('discount')->default(0)->comment('优惠力度type=1时固定金额 type=2时为0 type=3时为折扣比例');
            $table->unsignedInteger('startAt')->default(0)->comment('useTimeType=1时填写生效时间 为2时代表几天以后生效');
            $table->unsignedInteger('endAt')->default(0)->comment('useTimeType=1时填写失效时间 为2时代表几天以后失效');
            $table->unsignedInteger('useCondition')->default(0)->comment('使用满足条件 分');
            $table->unsignedTinyInteger('useMax')->default(0)->comment('最大使用张数，0不限制');
            $table->unsignedTinyInteger('availableSource')->default(0)->comment('0: 全部 1:POS 2:小程序 保存多个时需把对应数字相加');
            $table->unsignedTinyInteger('isSuperposeCoupon')->default(0)->comment('是否可以叠加其他优惠券 0: 否 1：是');
            $table->unsignedTinyInteger('isSuperposeActivity')->default(0)->comment('是否可以叠加其他活动 0: 否 1：是');
            $table->string('unableDate')->default('')->comment('不可用日期 json串 1~7代表周一到周末 8代表法定节假日');
            $table->string('unableTime')->default('')->comment('不可用时间json串，包含开始时间和结束时间的二维数组');
            $table->unsignedTinyInteger('isWechatNotice')->default(0)->comment('是否卫星提醒 0：否 1：是');
            $table->unsignedTinyInteger('isSmsNotice')->default(0)->comment('是否短信提醒 0：否 1：是');
            $table->unsignedTinyInteger('notifyAheadDays')->default(0)->comment('提前几天提醒');
            $table->unsignedBigInteger('createdByUid')->default(0)->comment('创建人Id');
            $table->unsignedBigInteger('updatedByUid')->default(0)->comment('更新人Id');
            $table->tinyInteger("status")->default(0)->comment("状态,0:未使用,1:已核销,2:已锁定,3:其他");
            $table->tinyInteger("isDeleted")->default(0)->comment("是否删除,0:未删除,1:已删除");
            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('更新时间');
            $table->index('status','idx_status');
            $table->index("createdAt", "idx_created_at");
            $table->index('availableSource','idx_availableSource');

            $table->collation = 'utf8mb4_general_ci';
        });

        $prefix = config('other.DB_PREFIX', '');
        DB::statement("ALTER TABLE `{$prefix}customer_coupons` COMMENT = '用户优惠券表'"); //表注释
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customer_coupons');
    }
}
